'''MisacaConvert

Author: Cauly Kan

Date: 2001.10.19

wiki: /wiki/MisacaConverterDesign.wiki

contact: cauliflower.kan@gmail.com

'''

import os

def MisacaConvert(inv, output_format, out, config):
    
    ff = config["ffmpeg"]
    
    cmd = ff + " -i %s -sameq -y -f %s %s" % (inv, output_format, out)
    
    print "{:>20}".format("MisacaConvert") + ": Converting video to format '%s'." % output_format
    
    #log = open("MisacaConvert.log", "a")
    
    (sin, sout, serr) = os.popen3(cmd)
    
    #log.write(cmd + sout.read() + serr.read())
    
    #log.close()
    
    return out

def MisacaContact(files, output, config):
    
    if _unix():
        
        return _contact_unix(files, output, config)
        
    else:
        
        return _contact_nonunix(files, output, config)
    
def _unix():
    
    return os.popen("which mkfifo").readline() == "/usr/bin/mkfifo\n"
    
def _contact_nonunix(files, output, config):

    '''
        ffmpeg -i input1.flv -sameq input1.tmp.mpg
        ffmpeg -i input2.flv -sameq input2.tmp.mpg
        copy /b input1.tmp.mpg + input2.tmp.mpg output.tmp.mpg
        ffmpeg -i output.tmp.mpg -sameq -f flv output.tmp
    '''
    
    cmd = ""
    
    ff = config["ffmpeg"]
    
    for item in files:
    
        cmd = cmd + ff + "-i %s -sameq %s.tmp.mpg\n" % (item, item)
    
    cmd = cmd + "copy /b %s %s.tmp.mpg" % ("+".join([ item + ".tmp.mpg" for item in files]), output)
    
    cmd = cmd + ff + "-i %s.tmp.mpg -sameq -f flv %s" % (output, output)
    
    cmd = cmd + "del *.tmp.mpg"
    
    print "{:>20}".format("MisacaConvert") + ": joining pieces of videos (nonunix), this may take some more time."
    
    (sin, sout, serr) = os.popen3(cmd)
    
    return
    
def _contact_unix(files, output, config):
    
    '''
    mkfifo all.a
    mkfifo all.v
    mkfifo temp1.a
    mkfifo temp1.v
    ffmpeg -i input1.flv -vn -f u16le -acodec pcm_s16le -ac 2 -ar 44100 - > temp1.a < /dev/null &
    ffmpeg -i input1.flv -an -f yuv4mpegpipe - > temp1.v < /dev/null &
    mkfifo temp2.a
    mkfifo temp2.v
    ffmpeg -i input2.flv -vn -f u16le -acodec pcm_s16le -ac 2 -ar 44100 - > temp2.a < /dev/null &
    { ffmpeg -i input2.flv -an -f yuv4mpegpipe - < /dev/null | tail -n +2 > temp2.v ; } &
    cat temp1.a temp2.a > all.a &
    cat temp1.v temp2.v > all.v &
    ffmpeg -f u16le -acodec pcm_s16le -ac 2 -ar 44100 -i all.a \
           -f yuv4mpegpipe -i all.v \
           -sameq -y output.flv
    rm temp[12].[av] all.[av]
    
    '''
    
    ff = config["ffmpeg"]
    
    cmd = "mkfifo all.a\nmkfifo all.v\n"
    
    i = 1
    
    for item in files:
        
        cmd = cmd + "mkfifo \"%s.a\"\nmkfifo \"%s.v\"\n" % (item, item)
        
        cmd = cmd + ff + " -i \"%s\" -vn -f u16le -acodec pcm_s16le -ac 2 -ar 44100 - > \"%s.a\" < /dev/null &\n" % (item, item)
        
        if i == 1:
        
            cmd = cmd + ff + " -i \"%s\" -an -f yuv4mpegpipe - > \"%s.v\" < /dev/null &\n" % (item, item)
            
        else:
            
            cmd = cmd + "{ " + ff + " -i \"%s\" -an -f yuv4mpegpipe - < /dev/null | tail -n +2 > \"%s.v\" ; } &\n" % (item, item)
    
        i = i + 1
    
    cmd = cmd + "cat \"" + "\" \"".join([(item + ".a") for item in files]) + "\" > all.a &\n"
    
    cmd = cmd + "cat \"" + "\" \"".join([(item + ".v") for item in files]) + "\" > all.v &\n"
    
    cmd = cmd + ff + " -f u16le -acodec pcm_s16le -ac 2 -ar 44100 -i all.a -f yuv4mpegpipe -i all.v -sameq -y \"%s\"\n" % output
    
    cmd = cmd + "\n".join([("rm \"%s\".[av]" % item) for item in files]) + "\n"
    
    cmd = cmd + "rm all.[av]\n"
    
    print "{:>20}".format("MisacaConvert") + ": joining pieces of videos (unix), this may take some time."
    
    #log = open("MisacaConvert.log", "w")
    
    print cmd
    
    (sin, sout, serr) = os.popen3(cmd)
    
    #log.write(cmd + sout.read() + serr.read())
    
    #log.close()
    
    return output
